package com.jorado.etl.service;

import com.jorado.basedata.BaseDataUtils;
import com.jorado.core.Result;
import com.jorado.etl.model.ApplyEtlInput;
import com.jorado.event.EventClient;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import com.jorado.search.core.service.impl.FileExporter;
import org.springframework.context.annotation.Description;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;

@Description("基于应聘文本数据的语料抽取服务")
public final class ApplyFileFastTextEtlService extends ExportToFileImpl<String> {

    private int total;
    private ApplyEtlService etlService;

    public ApplyFileFastTextEtlService(String sourceFile, String distFile) {
        this(sourceFile, distFile, true);
    }

    public ApplyFileFastTextEtlService(String sourceFile, String distFile, boolean deleteIfExists) {
        super(new FileExporter(sourceFile, true), distFile, deleteIfExists);
        this.etlService = new ApplyEtlService(this.distFile);
        this.setAfterFilter(this::after);
    }

    /**
     * 用户id,性别,职位类别ID,简历中当前城市id,简历中学校id,职位发布城市id,专业id,职位编号,投递日期
     * 0,     1,   006,      622,          2962,      801,        614,  CC000106451J90000384000,2017-10-11 17:52:50.670
     *
     * @param dataList
     */
    @Override
    protected void export(List<String> dataList) {

        total += dataList.size();

        HashSet<ApplyEtlInput> etlList = new LinkedHashSet<>();

        for (String line : dataList) {

            String[] splits = line.split(",");

            ApplyEtlInput etlInput = new ApplyEtlInput();

            etlInput.setJobNum(splits[7]);
            etlInput.setJobTypeId(splits[2]);
            etlInput.setJobType(BaseDataUtils.getSubJobTypeName(etlInput.getJobTypeId()));
            //etlInput.setBigJobTypeId(BaseDataUtils.getParentTypeCode(etlInput.getJobTypeId()));
            //etlInput.setBigJobType(BaseDataUtils.getParentTypeName(etlInput.getJobTypeId()));
            etlInput.setSchoolId(splits[4]);
            // etlInput.setSchool(BaseDataUtils.getSchoolName(etlInput.getSchoolId()));
            etlInput.setMajorId(splits[6]);
            //etlInput.setMajor(BaseDataUtils.getMajorName1(etlInput.getMajorId()));
            etlList.add(etlInput);

        }

        this.etlService.save(etlList);

    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共抽取职位:[%d]个", total));

        return Result.OK;
    }
}
